/**
 * 
 */
package com.netscenery.yang.model;

import org.jdom2.Attribute;
import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.util.XMLTag;
import com.netscenery.yang.YangKeyword;
import com.netscenery.yang.YangStatement;
import com.netscenery.yang.designer.MapInfo;

/**
 * @author llly
 *
 */
public class Bit extends ModelElement {
	//public static final int DEFAULTPOS = -1;
	private int position;
	private boolean usedefaultpos = true;
	//���뵽bits�е�ʵ��λ��
	private int actualPostion;
	/**
	 * @return the actualPostion
	 */
	public int getActualPostion() {
		return actualPostion;
	}

	/**
	 * @param actualPostion the actualPostion to set
	 */
	public void setActualPostion(int actualPostion) {
		this.actualPostion = actualPostion;
	}

	public boolean isUsedefaultpos() {
		return usedefaultpos;
	}

	private String description;
	private String reference;
	private Status status = Status.CURRENT;
	
	

	
	public Bit(String name){
		setName(name);
		usedefaultpos = true;
	}	
	public Bit(String name, int position){
		setName( name);
		this.position = position;
		usedefaultpos = false;
	}
	
	

	public int getPosition() {
		return position;
	}


	public void setPosition(int position) {
		if (this.position < 0){
			return;
		}
		this.position = position;
		usedefaultpos = false;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getReference() {
		return reference;
	}


	public void setReference(String reference) {
		this.reference = reference;
	}


	public Status getStatus() {
		return status;
	}


	public void setStatus(Status status) {
		if (null == status){
			return;
		}
		switch(status){
		case DEFAULT:
		{
			return;
		}
		default:{
			this.status = status;
		}
		}
		
	}


	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#validate()
	 */
	@Override
	public Validator validate() {
		Validator validator = new Validator();
		if (0 > this.position){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"position is less than zero"));
		}
		if (!checkName(this.name)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,ErrorCode.COMMON_INVALIDNAME));

		}
		Validator v = super.validate();
		validator.copy(v);

		return validator;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toYang()
	 */
	@Override
	public YangStatement toYang() {
		YangStatement bit = new YangStatement(YangStatement.YANG_STATEMENT_BIT,this.name);
		//���λ����Ĭ�ϵģ�����Ҫת��YANG���
		if (false == this.usedefaultpos){
			YangStatement position = new YangStatement(YangStatement.YANG_STATEMENT_POSITION,
					new Integer(this.position).toString());
			bit.addChild(position);
		}
		
		if (!isNullString(this.description)){
			YangStatement descr = new YangStatement(YangStatement.YANG_STATEMENT_DESCRIPTION,
					this.description);
			bit.addChild(descr);
		}
		
		if (!isNullString(this.reference)){
			YangStatement ref = new YangStatement(YangStatement.YANG_STATEMENT_REFERENCE,
					this.reference);
			bit.addChild(ref);
		}
		//���״̬��CURRENT,�����
		if(!Status.CURRENT.equals(this.status)){
			YangStatement status = new YangStatement(YangStatement.YANG_STATEMENT_STATUS,this.status.getStatus());
			bit.addChild(status);
		}
		return bit;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#toXML()
	 */
	@Override
	public Element toXML() {
		Element bit = new Element(XMLTag.XML_TAG_BIT);
		Element name = new Element(XMLTag.XML_TAG_NAME);
		name.setText(this.name);
		bit.addContent(name);
//		if (false == this.usedefaultpos){
//			Element position = new Element(XMLTag.XML_TAG_POSITION);
//			position.setText(new Integer(this.position).toString());
//		}
		Element position = new Element(XMLTag.XML_TAG_POSITION);
		position.setText(new Integer(this.getActualPostion()).toString());
		bit.addContent(position);
		
		if (null != this.description){
			Element descr = new Element(XMLTag.XML_TAG_DESCRIPTION);
			descr.setText(this.description);
			bit.addContent(descr);
		}
		
		if (null != this.reference){
			Element ref = new Element(XMLTag.XML_TAG_REFERENCE);
			ref.setText(this.reference);
			bit.addContent(ref);
		}
		if(!Status.CURRENT.equals(this.status)){
			Element status = new Element(XMLTag.XML_TAG_STATUS);
			status.setText(this.status.getStatus());
			bit.addContent(status);
		}
//		Element status = new Element(XMLTag.XML_TAG_STATUS);
//		status.setText(this.status.getStatus());
//		bit.addContent(status);
		return bit;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#isFieldMandatory(java.lang.String)
	 */
	@Override
	public boolean isFieldMandatory(String field) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readXML(org.jdom2.Element)
	 */
	@Override
	public int readXML(Element element) {
		if (null == element){
			return MODELELEMENT_OK;
		}
		String keyword = element.getName();
		String value = element.getText();
		if (keyword.equals(XMLTag.XML_TAG_POSITION)){
			if (null == value){
				this.usedefaultpos = true;
			}
			else {
				this.usedefaultpos = false;
				this.position = new Integer(value).intValue();
			}
		}
		else if (keyword.equals(XMLTag.XML_TAG_DESCRIPTION)){
			this.description = value;
		}
		else if (keyword.equals(XMLTag.XML_TAG_REFERENCE)){
			this.reference = value;
		}
		else if (keyword.equals(XMLTag.XML_TAG_STATUS)){
			this.status = Status.getStatus(value);
		}
		else if (keyword.equals(XMLTag.XML_TAG_NAME)){
			return MODELELEMENT_OK;
		}
		else {
			return MODELELEMENT_FAIL;
		}
		return MODELELEMENT_OK;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.ModelElement#readYANG(com.netscenery.modeldesigner.util.YangStatement)
	 */
	@Override
	public int readYANG(YangStatement statement) {
		if (null == statement){
			return MODELELEMENT_OK;
		}
		String value = statement.getArgument();
		if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_POSITION)){
			if (null == value){
				this.usedefaultpos = true;
			}
			else {
				this.usedefaultpos = false;
				this.position = new Integer(value).intValue();
			}
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			this.description = value;
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			this.reference = value;
		}
		else if (statement.getKeyword().equals(YangStatement.YANG_STATEMENT_STATUS)){
			this.status = Status.getStatus(value);
		}
		else {
			return MODELELEMENT_FAIL;
		}
		return MODELELEMENT_OK;
	}

	public  MapInfo getMapInfo() throws SecurityException, NoSuchMethodException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Element toYin() {
		Element bit = new Element(YangKeyword.BIT.getKeyword(),env.getNs());
		Attribute name = new Attribute(XMLTag.XML_TAG_NAME,this.name);
		bit.setAttribute(name);
		//���λ����Ĭ�ϵģ�����Ҫת��YANG���
		if (false == this.usedefaultpos){
			Element position = new Element(YangKeyword.POSITION.getKeyword(),env.getNs());
			Attribute value = new Attribute(YangKeyword.VALUE.getKeyword(),new Integer(this.position).toString());
			position.setAttribute(value);
			bit.addContent(position);
		}
		
		if (!isNullString(this.description)){
			Element descr = new Element(YangStatement.YANG_STATEMENT_DESCRIPTION,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.description);
			descr.addContent(text);
			bit.addContent(descr);
		}
		
		if (!isNullString(this.reference)){
			Element ref = new Element(YangStatement.YANG_STATEMENT_REFERENCE,env.getNs());
			Element text = new Element(XMLTag.XML_TAG_TEXT,env.getNs());
			text.setText(this.reference);
			ref.addContent(text);
			bit.addContent(ref);
		}
		//���״̬��CURRENT,�����
		if(!Status.CURRENT.equals(this.status)){
			Element status = new Element(YangStatement.YANG_STATEMENT_STATUS,env.getNs());
			Attribute value = new Attribute(YangKeyword.VALUE.getKeyword(),this.getStatus().getStatus());
			status.setAttribute(value);
			bit.addContent(status);
		}
		return bit;
	}
	@Override
	public int readYin(Element element) throws ModelException {
		if (null == element){
			return MODELELEMENT_OK;
		}
		if (element.getName().equals(YangStatement.YANG_STATEMENT_POSITION)){
			String value= element.getAttributeValue(YangKeyword.VALUE.getKeyword());
			if (null == value){
				return MODELELEMENT_FAIL;
			}
			else {
				this.usedefaultpos = false;
				this.position = new Integer(value).intValue();
			}
		}
		else if (element.getName().equals(YangStatement.YANG_STATEMENT_DESCRIPTION)){
			Element text = element.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setDescription(text.getText());
		}
		else if (element.getName().equals(YangStatement.YANG_STATEMENT_REFERENCE)){
			Element text = element.getChild(XMLTag.XML_TAG_TEXT,env.getNs());
			if (null == text){
				return MODELELEMENT_FAIL;
			}
			this.setReference(text.getText());
		}
		else if (element.getName().equals(YangStatement.YANG_STATEMENT_STATUS)){
			String value= element.getAttributeValue(YangKeyword.VALUE.getKeyword());
			if (null == value){
				return MODELELEMENT_FAIL;
			}
			else {
				this.status = Status.getStatus(value);
			}
			
		}
		else {
			return MODELELEMENT_FAIL;
		}
		return MODELELEMENT_OK;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}


}
